package com.starbaba.spreadChannelManager.service;


import java.util.Map;

import com.base.common.database.BaseDao;
import com.base.common.exception.AppException;
import com.starbaba.core.web.struts2.BaseAction;
import com.starbaba.model.dictionary.Constant;
import com.starbaba.spreadChannelManager.model.ProductCodeModel;
import com.starbaba.spreadChannelManager.model.SpreadChannelModel;

public class SpreadChannelService extends BaseAction{
	
	private BaseDao testDao = new BaseDao(Constant.POOL_LIFE);
	
	public boolean addNewChannel(SpreadChannelModel model) throws AppException {
		
		boolean flag = false;
		try {
			
			StringBuffer sbSql = new StringBuffer();
			sbSql.append("insert into loan.spread_channel_manager(prd_id,prd_name,spread_link,status)");
			sbSql.append(" values(?,?,?,?)");
			
			
			//推广链接
			long key = testDao.addReturnGeneratedKey(sbSql.toString(), new Object[]{
												model.getPrdId(),
												model.getPrdName(),
												model.getSpreadLink(),
												model.getStatus()
												});
			if (key > 0) {
				flag = true;
			}
			
		} catch(Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
	
	public boolean updateChannel(Map<String, Object> paramsMap) throws AppException {
		
		boolean flag = false;
		
		try{
			int id = Integer.parseInt((String) paramsMap.get("id"));
			int prdId = Integer.parseInt((String) paramsMap.get("prdId"));
			
			String sql = "select isapp,prdid,name from comm_product_prdid where prdid=?";
			ProductCodeModel tempModel = testDao.get(sql, ProductCodeModel.class, prdId);
			String shortLink = Constant.COMMON_HOST + "/frontend_loan_service/common?funid=1&appid=3&service=loan_service&isapp="+tempModel.getIsapp()+"&prdid="+prdId;
			//推广链接
			StringBuffer sbSql = new StringBuffer();
			sbSql.append("update loan.spread_channel_manager");
			sbSql.append(" set prd_name = ?,prd_id=?,spread_link=?");
			sbSql.append(" where id = ?");
			int num = testDao.update(sbSql.toString(), new Object[]{
														tempModel.getName(),
														tempModel.getPrdid(),
														shortLink,
														id
												});
			
			if(num > 0){
				flag = true;
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
	
	//判断产品id是否存在
	public boolean isExist(int prdId,String prdName) {
		
		boolean flag = true;
		
		try{
			
			//推广链接
			StringBuffer sbSql = new StringBuffer();
			sbSql.append("select prd_id");
			sbSql.append(" from loan.spread_channel_manager");
			sbSql.append(" where prd_id = ?");
			
			SpreadChannelModel model = testDao.get(sbSql.toString(), SpreadChannelModel.class, prdId);
			
			if (model == null) {
				flag = false;
			}
			
		} catch(Exception e) {
			e.printStackTrace();
		}
		
		return flag;
		
	}
	
	public boolean isUpdate(int id,int prdId) {
		
		boolean flag = true;
		
		try {
			
			StringBuffer sbSql = new StringBuffer();
			sbSql.append("select id");
			sbSql.append(" from loan.spread_channel_manager");
			sbSql.append(" where prd_id = ? ");
			
			SpreadChannelModel model = testDao.get(sbSql.toString(), SpreadChannelModel.class, prdId);
			
			if(model == null) {
				flag = false;
			}

		} catch(Exception e) {
			e.printStackTrace();
		}
		
		return flag;
		
	}

	public boolean dropChannel(Map<String, Object> paramsMap) {
		
		boolean flag = false;
		
		try {
			int id = Integer.parseInt((String) paramsMap.get("id"));
			int prdId = Integer.parseInt((String) paramsMap.get("prdId"));
			StringBuffer sbSql = new StringBuffer();
			sbSql.append("update loan.spread_channel_manager");
			sbSql.append(" set status = 0");
			sbSql.append(" where id = ?");
			
			int num = testDao.update(sbSql.toString(), new Object[]{id});
			
			if (num > 0) {
				flag = true;
			}
			
		} catch(Exception e) {
			e.printStackTrace();
		}
		
		return flag;
	}
	
}
